"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 去除审计日志写权限
Create At   : 2020/07/20
Owner       : @li-xin12345
Description :
    1.更改审计日志权限，去除其写权限
    2.重启数据库
Expect      :
    1. 更改审计日志权限，去除其写权限成功
    2. 数据库启动成功
History     :
    Modified: 2020/12/19  modified by @wan005,tearDown顺序不正确，应先恢复文件权限再重启数据库
    Modified: 2020/12/19  modified by @wan005,重启失败断言不完善，
    优化脚本增加‘Failed to start cluster’
    Modified: 2022/09/20  modified by @wan005,优化重启期望
"""

import time
import unittest
from yat.test import Node
from testcase.utils.CommonSH import *
import sys
from yat.test import macro
from testcase.utils.CommonSH import *

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

logger = Logger()
sys.path.append(sys.path[0] + "/../")


class IndexFileDamaged(unittest.TestCase):
    sh_primy = CommonSH('PrimaryDbUser')
    sh_stadb = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        logger.info('------------Opengauss_Reliability_Dbsys_Case209.py start------------')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.DB_AUDIT_PATH = macro.PG_AUDIT_PATH
        self.rootNode = Node('default')
        self.primaryNode = Node(node='PrimaryDbUser')
        self.Constant = Constant()

    def test_Index_file_damaged(self):
        # 关闭正在运行的数据库
        logger.info('----------------------------关闭数据库-----------------------------')
        excute_cmd = f'''
                        source {self.DB_ENV_PATH};
                        gs_om -t stop;
                    '''
        logger.info(excute_cmd)
        msg = self.primaryNode.sh(excute_cmd).result()
        logger.info(msg)
        self.assertIn(self.Constant.STOP_SUCCESS_MSG, msg)

        # 更改审计日志目录权限
        logger.info('----------------------------去除审计日志目录写权限-----------------------------')
        excute_cmd = f'chmod -R 500 {self.DB_AUDIT_PATH} ;'
        logger.info(excute_cmd)
        msg = self.rootNode.sh(excute_cmd).result()
        logger.info(msg)

        # 开启数据库验证是否受数据库实例目录权限影响
        logger.info('----------------------------开启数据库-----------------------------')
        excute_cmd = f'''
                        source {self.DB_ENV_PATH};
                        gs_om -t stop ;
                        gs_om -t start;
                        '''
        logger.info(excute_cmd)
        msg = self.primaryNode.sh(excute_cmd).result()
        logger.info(msg)
        self.assertIn("Successfully started", msg)

        # 开启数据库验证是否受数据库实例目录权限影响
        logger.info('----------------------------查看数据库状态-----------------------------')
        excute_cmd = f'''
                                source {self.DB_ENV_PATH};
                                gs_om -t status --detail;
                                '''
        logger.info(excute_cmd)
        msg = self.primaryNode.sh(excute_cmd).result()
        logger.info(msg)
        self.assertNotIn(self.Constant.UNAVAILABLE_STATUS, msg)

        # 恢复数据库实例目录权限
        logger.info('----------------------------增加数据库实例目录写权限-----------------------------')
        excute_cmd = f'chmod -R 700 {self.DB_AUDIT_PATH} ;'
        logger.info(excute_cmd)
        msg = self.rootNode.sh(excute_cmd).result()
        logger.info(msg)

        # 再次开启数据库验证是否受数据库实例目录权限影响
        logger.info('----------------------------再次开启数据库-----------------------------')
        excute_cmd = f'''
                                source {self.DB_ENV_PATH};
                                gs_ctl restart -D {self.DB_INSTANCE_PATH};
                                gs_om -t status --detail;                    
                                '''
        logger.info(excute_cmd)
        msg = self.primaryNode.sh(excute_cmd).result()
        logger.info(msg)
        self.assertIn(self.Constant.START_STATUS_MSG, msg)

    def tearDown(self):
        logger.info('----------------------------后置处理-----------------------------')
        excute_cmd = f'chmod -R 700 {self.DB_AUDIT_PATH} ;'
        logger.info(excute_cmd)
        msg = self.rootNode.sh(excute_cmd).result()
        logger.info(msg)
        time.sleep(2)

        db_status = self.sh_primy.getDbInstanceStatus()
        logger.info(db_status)
        if not db_status:
            is_started = self.sh_primy.startDbCluster()
            self.assertTrue(is_started)
            logger.info(f'{is_started}')
            logger.info(f'db_status: {is_started}')

        logger.info('-----------------------Reliability_Dbsys_Case209执行完毕----------------------')
